// pages/invoice/my-invoice/my-invoice.js
var httpUtil = require('../../../utils/httpUtil.js');
var uiUtil = require('../../../utils/uiUtil.js');
const app = getApp();
var that;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    invoiceLists:{
      totalCount: null,
      pageCount: null,
      pageNo: "0",
      pageSize: "30",
      dataList: []
    },

    requestGoodsTask: null, //请求task
    isLoadingMore: false,
    enableLoadMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    that.mySearchBill();
  },

  /**
   * 我的发票查询
   */
  mySearchBill() {
    let pageNo = (parseInt(that.data.invoiceLists.pageNo) + 1).toString();
    let pageSize = that.data.invoiceLists.pageSize;
    let dataList = that.data.invoiceLists.dataList;
    let totalCount = that.data.invoiceLists.totalCount;

    var lastRequestTask = that.data.requestGoodsTask;
    if (lastRequestTask) {
      try {
        lastRequestTask.abort()
      } catch (e) { }
    }

    var curPage = that.data.invoiceLists.pageNo
    if (curPage != 0 && (!that.data.enableLoadMore || that.data.isLoadingMore)) { //不可加载||正在加载
      return;
    }
    
    var requestTask = httpUtil.PostRequest("searchBill", {
      pageNo: pageNo,
      pageSize: pageSize
    }, {
        success(res) {
          if (0 == that.data.invoiceLists.totalCount) {
            res.dataList = res.dataList || []
            if (res.dataList.length == 0) { //内容为空
              that.setData({
                fail: true
              })
            }
          } else {
            res.dataList = dataList.concat(res.dataList || []);
          }

          that.setData({
            invoiceLists: res,
            enableLoadMore: res.dataList.length < res.totalCount,
            touchThat: null
          })
        },
        fail(code, res) {
          that.setData({
            enableLoadMore: false,
          })
          uiUtil.showToast(res);
        },
        complete() {
          that.setData({
            isLoadingMore: false
          })
        }
      })
    that.setData({
      requestGoodsTask: requestTask
    })
  },

  /**
   * 我的发票删除
   */
  deleteInvoice(e){
    wx.showModal({
      title: '删除发票',
      content: '确定要删除该发票吗?',
      showCancel: true,
      success(res) {
        if (res.confirm){
          let id = e.currentTarget.dataset.id;
          httpUtil.PostRequest("delBill", {
            id:id
          }, {
              success(res) {
                that.onResultRegisterSuccess();
              },
              fail(code, res) {
                uiUtil.showToast(res);
              },
              complete() { }
            })
        }
      }
    })
  },


  /**
  * 触底加载
  */
  onReachBottom() {
    that.mySearchBill();
  },

  /**
  * 回调重新加载
  */
  onResultRegisterSuccess() {
    that.setData({
      invoiceLists: {
        totalCount: null,
        pageCount: null,
        pageNo: "0",
        pageSize: "30",
        dataList: []
      },
      requestGoodsTask: null, //请求task
      isLoadingMore: false,
      enableLoadMore: true
    })
    that.mySearchBill();
  },


  /**
   * 左滑删除
   */
  touchS: function (e) {
    if (e.touches.length == 1) {
      this.setData({
          //设置触摸起始点水平方向位置
          startX: e.touches[0].clientX
      });
    }
  },

  touchE: function (e) {
    if (e.changedTouches.length == 1) {
      //手指移动结束后水平位置
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离
      var disX = this.data.startX - endX;
      // //获取手指触摸的是哪一项
      var index = e.currentTarget.dataset.index;
      if(disX>50){
        that.setData({
          touchThat: index
        });
      } else if(disX<-50){
        that.setData({
          touchThat: null
        });
      }
    }
  },
})